home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10545 < prev    next >
Encoding:
Text File  |  1996-08-05  |  15.8 KB  |  614 lines

  1. Path: medtronic.com!usenet
  2. From: Peter Joseph Kovach <peter.joseph.kovach@medtronic.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: NEED HELP ON A WINDOWS BITMAP BUTTON IN A DIALOG BOX PROBLEM !!
  5. Date: 8 Mar 1996 16:10:47 GMT
  6. Organization: Medtronic
  7. Message-ID: <4hpm67$3pt@gazette.medtronic.com>
  8. NNTP-Posting-Host: 144.15.63.237
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  13.  
  14. In the following code, the system keeps crashing when I do the autoload 
  15. of the bitmap button.  (Please look in the code below for the #ifdef PETE
  16. line.   It is there.
  17.  
  18. Is there something else I need to do to initialize the button.  It seems 
  19. many of its, and potentially its parents, values are not initialized so 
  20. they are garbage.
  21.  
  22. If you can help,  please let me know and I could send potentially send
  23. you the whole project if you needed it.  The code compiles, but when it 
  24. attempts to generate this dialog box, it dies.
  25.  
  26. Thank you in advance!
  27.  
  28.  
  29.  
  30.  
  31. #include "stdafx.h"
  32. #include <stdio.h>
  33. #include <process.h>
  34. #include "resource.h"
  35.  
  36. #include "introtst.h"
  37. #include "mainfrm.h"
  38. #include "introdoc.h"
  39. #include "introvw.h"
  40. #include "introscr.h"
  41.  
  42. #ifdef _DEBUG
  43. #undef THIS_FILE
  44. static char BASED_CODE THIS_FILE[] = __FILE__;
  45. #endif
  46.  
  47. CMainFrame  *pMainFrame;
  48. #define NUMENTRIES 128
  49. CBrush         pBrush;
  50. HPALETTE hpal;
  51. PALETTEENTRY ape[NUMENTRIES]; 
  52. int red, green, blue;
  53. int cColors = NUMENTRIES;
  54. CRect *lpr = NULL, *UpRect;
  55.  
  56.     LOGFONT lf, lf2;
  57.     PAINTSTRUCT ps;      // paint structure
  58.        CClientDC *dc;    
  59.  
  60.  
  61. /* Create the "---" pen */
  62.  
  63. HPEN hDashPen = CreatePen(PS_DASH,                  /* style */
  64.            1,                                       /* width */
  65.            RGB(0, 0, 0));                           /* color */
  66.  
  67. /* Create the standard pen */
  68.  
  69. HPEN hSolidPen = CreatePen(PS_SOLID,                /* style */
  70.            1,                                       /* width */
  71.            RGB(0, 0, 0));                           /* color */
  72.  
  73. // Create a white pen
  74. HPEN hWhitePen = CreatePen(PS_SOLID,                /* style */
  75.            1,                                       /* width */
  76.            RGB(255, 255, 255));                     /* color */
  77.  
  78. /* Create the "->->->" pen */
  79.  
  80. HPEN hDotPen = CreatePen(2,                  /* style */
  81.     1,                                       /* width */
  82.     RGB(0, 0, 0));                           /* color */
  83.  
  84.  
  85. // Draw something
  86. HPEN hOldPen;          // old pen handle
  87.  
  88. introscreen *intro;
  89.  
  90.  
  91.  
  92. /////////////////////////////////////////////////////////////////////////
  93. ////
  94. // CIntrotstApp
  95.  
  96. BEGIN_MESSAGE_MAP(CIntrotstApp, CWinApp)
  97.     //{{AFX_MSG_MAP(CIntrotstApp)
  98.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  99.         // NOTE - the ClassWizard will add and remove mapping 
  100. macros here.
  101.         //    DO NOT EDIT what you see in these blocks of 
  102. generated code!
  103.     //}}AFX_MSG_MAP
  104.     // Standard file based document commands
  105.     ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  106.     ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  107.     // Standard print setup command
  108.     ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  109. END_MESSAGE_MAP()
  110.  
  111. /////////////////////////////////////////////////////////////////////////
  112. ////
  113. // CIntrotstApp construction
  114.  
  115. CIntrotstApp::CIntrotstApp()
  116. {
  117.     // TODO: add construction code here,
  118.     // Place all significant initialization in InitInstance
  119. }
  120.  
  121. /////////////////////////////////////////////////////////////////////////
  122. ////
  123. // The one and only CIntrotstApp object
  124.  
  125. CIntrotstApp NEAR theApp;
  126.  
  127. /////////////////////////////////////////////////////////////////////////
  128. ////
  129. // CIntrotstApp initialization
  130.  
  131. BOOL CIntrotstApp::InitInstance()
  132. {
  133.     // Standard initialization
  134.     // If you are not using these features and wish to reduce the 
  135. size
  136.     //  of your final executable, you should remove from the 
  137. following
  138.     //  the specific initialization routines you do not need.
  139.  
  140.     SetDialogBkColor();        // Set dialog background color to gray
  141.     LoadStdProfileSettings();  // Load standard INI file options 
  142. (including MRU)
  143.  
  144.     // Register the application's document templates.  Document 
  145. templates
  146.     //  serve as the connection between documents, frame windows and 
  147. views.
  148.  
  149.     CMultiDocTemplate* pDocTemplate;
  150.     pDocTemplate = new CMultiDocTemplate(
  151.         IDR_INTROTTYPE,
  152.         RUNTIME_CLASS(CIntrotstDoc),
  153.         RUNTIME_CLASS(CMDIChildWnd),        // standard MDI child 
  154. frame
  155.         RUNTIME_CLASS(CIntrotstView));
  156.     AddDocTemplate(pDocTemplate);
  157.  
  158.     // create main MDI Frame window
  159.     pMainFrame = new CMainFrame; 
  160.     int test = IDR_MAINFRAME;
  161.     if (!pMainFrame->LoadFrame(IDR_MAINFRAME, 
  162. WS_VISIBLE|WS_OVERLAPPEDWINDOW, NULL, NULL))
  163.         return FALSE;
  164.     m_pMainWnd = pMainFrame;
  165.  
  166.     if (m_lpCmdLine[0] != '\0')
  167.     {
  168.         // TODO: add command line processing here
  169.     }
  170.  
  171.     // The main window has been initialized, so show and update it.
  172.     pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
  173.     pMainFrame->UpdateWindow(); 
  174.     
  175.     // create a new (empty) document
  176.     //OnFileNew(); 
  177.     intro = new introscreen(pMainFrame);
  178.     intro->ShowWindow(SW_SHOWNORMAL);
  179.     intro->UpdateWindow();
  180.     pMainFrame->UpdateWindow();
  181.  
  182.     // The following initializes a LOGPALETTE structure and an array 
  183. of 
  184.     // PALETTEENTRY structures, and then uses the CreatePalette 
  185. function
  186.     // to retrieve a handle of a logical palette: 
  187.  
  188.     LOGPALETTE *plgpl = (LOGPALETTE*) LocalAlloc(LPTR,
  189.         sizeof(LOGPALETTE) + cColors * sizeof(PALETTEENTRY));
  190.  
  191.      plgpl->palNumEntries = cColors;
  192.     plgpl->palVersion = 0x300;
  193.  
  194.     for (int i = 0, red = 0, green = 127, blue = 127; i < NUMENTRIES;
  195.         i++, red += 1, green += 1, blue += 1) 
  196.         {
  197.             ape[i].peRed =
  198.           plgpl->palPalEntry[i].peRed = LOBYTE(red);
  199.           ape[i].peGreen =
  200.             plgpl->palPalEntry[i].peGreen = LOBYTE(green);
  201.           ape[i].peBlue =
  202.             plgpl->palPalEntry[i].peBlue = LOBYTE(blue);
  203.           ape[i].peFlags =
  204.             plgpl->palPalEntry[i].peFlags = PC_RESERVED;
  205.         }
  206.         
  207.         
  208.     hpal = CreatePalette((LPLOGPALETTE)plgpl); 
  209.     if (!hpal) 
  210.     {
  211. //       Error("CreatePalette() failed");
  212.        return -1;
  213.     }
  214.     SelectPalette((HDC)&(intro->dc), hpal, 0);  //(HDC)dcTest->m_hDC
  215.     RealizePalette((HDC)&(intro->dc));
  216.     
  217.     // Create a solid fill brush pattern to fill the chart data
  218.     pBrush.CreateSolidBrush(PALETTEINDEX(0));
  219.  
  220.     // Finally free the palette
  221.     LocalFree((HLOCAL) plgpl);
  222.  
  223.  
  224.     lpr = new CRect(); 
  225.  
  226.  
  227.     return TRUE;
  228. }
  229.  
  230. /////////////////////////////////////////////////////////////////////////
  231. ////
  232. // CAboutDlg dialog used for App About
  233.  
  234. class CAboutDlg : public CDialog
  235. {
  236. public:
  237.     CAboutDlg();
  238.  
  239. // Dialog Data
  240.     //{{AFX_DATA(CAboutDlg)
  241.     enum { IDD = IDD_ABOUTBOX };
  242.     //}}AFX_DATA
  243.  
  244. // Implementation
  245. protected:
  246.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 
  247. support
  248.     //{{AFX_MSG(CAboutDlg)
  249.         // No message handlers
  250.     //}}AFX_MSG
  251.     DECLARE_MESSAGE_MAP()
  252. };
  253.  
  254. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  255. {
  256.     //{{AFX_DATA_INIT(CAboutDlg)
  257.     //}}AFX_DATA_INIT
  258. }
  259.  
  260. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  261. {
  262.     CDialog::DoDataExchange(pDX);
  263.     //{{AFX_DATA_MAP(CAboutDlg)
  264.     //}}AFX_DATA_MAP
  265. }
  266.  
  267. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  268.     //{{AFX_MSG_MAP(CAboutDlg)
  269.         // No message handlers
  270.     //}}AFX_MSG_MAP
  271. END_MESSAGE_MAP()
  272.  
  273. // App command to run the dialog
  274. void CIntrotstApp::OnAppAbout()
  275. {
  276.     CAboutDlg aboutDlg;
  277.     aboutDlg.DoModal();
  278. }
  279.  
  280. /////////////////////////////////////////////////////////////////////////
  281. ////
  282. // CIntrotstApp commands
  283.  
  284.  
  285. // Implement CDialog derived IntroScreen putting bitmap into background
  286. // of a dialog box with StretchBlt() in WM_ERASEBKGND handler.
  287. //
  288. /////////////////////////////////////////////////////////////////////////
  289. ////
  290. introscreen::introscreen(CWnd* pParent /*=NULL*/)
  291.     : CDialog(introscreen::IDD, pParent)
  292. {
  293.     //{{AFX_DATA_INIT(introscreen)
  294.         // NOTE: the ClassWizard will add member initialization 
  295. here
  296.     //}}AFX_DATA_INIT 
  297.     
  298.     Create( IDD_SELECTPROG , NULL);
  299.  
  300.     intro = this;
  301. }
  302.  
  303. introscreen::~introscreen()
  304. {
  305.     delete button1;
  306.     delete button2;
  307.     delete button3;
  308.     delete button4;
  309. }
  310.  
  311. void introscreen::DoDataExchange(CDataExchange* pDX)
  312. {
  313.     CDialog::DoDataExchange(pDX);
  314.     //{{AFX_DATA_MAP(introscreen)
  315.     DDX_Control(pDX, IDC_IDST, m_IDST);
  316.     DDX_Control(pDX, IDC_IDITR, m_IDITR);
  317.     DDX_Control(pDX, IDC_IDINS, m_IDINS);
  318.     DDX_Control(pDX, IDC_IDDP, m_IDDP);
  319.     //}}AFX_DATA_MAP
  320. }
  321.  
  322. BEGIN_MESSAGE_MAP(introscreen, CDialog)
  323.     //{{AFX_MSG_MAP(introscreen)
  324.     ON_BN_CLICKED(IDC_IDDP, OnIddp)
  325.     ON_BN_CLICKED(IDC_IDINS, OnIdins)
  326.     ON_BN_CLICKED(IDC_IDITR, OnIditr)
  327.     ON_BN_CLICKED(IDC_IDST, OnIdst)
  328.     ON_WM_CTLCOLOR()
  329.     ON_WM_DESTROY()
  330.     ON_WM_CREATE()
  331.     ON_WM_SHOWWINDOW()
  332.     //}}AFX_MSG_MAP
  333. END_MESSAGE_MAP()
  334.  
  335.  
  336. /////////////////////////////////////////////////////////////////////////
  337. ////
  338. // introscreen message handlers
  339.  
  340. void introscreen::OnIddp()
  341. {
  342.     // TODO: Add your control notification handler code here
  343.     
  344. }
  345.  
  346. void introscreen::OnIdins()
  347. {
  348.     // TODO: Add your control notification handler code here
  349.     
  350. }
  351.  
  352. void introscreen::OnIditr()
  353. {
  354.     // TODO: Add your control notification handler code here
  355.     
  356. }
  357.  
  358. void introscreen::OnIdst()
  359. {
  360.     // TODO: Add your control notification handler code here
  361.     
  362. }
  363.  
  364. //***********************************************************************
  365. // IntroScreen::OnCtlColor()
  366. //
  367. // Purpose:
  368. //
  369. //        SetBkMode() to TRANSPARENT so text drawing won't use dialogs
  370. //        background color.
  371. //        Return a handle to a hollow brush, so no other piece of the
  372. //        bitmap in the background will be erased.
  373. //
  374. // Parameters:
  375. //
  376. //        CDC* pDC        - Device context for drawing control
  377. //        CWnd* pWnd        - Pointer to control CWnd
  378. //        UINT nCtlColor    - Control type (ie. CTLCOLOR_STATIC)
  379. //
  380. // Returns:
  381. //
  382. //        HBRUSH    (Handle of background brush to paint background with)
  383. //
  384. // Comments:
  385. //
  386. //        This function could be left.  The main difference will be that
  387. //        text will show up with a block of the background color around 
  388. it.
  389. //
  390.  
  391. // History:
  392. //
  393. //   Date   Comment                                           Initials
  394. // ======== ================================================= ========
  395. //***********************************************************************
  396. HBRUSH introscreen::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
  397. {
  398.     // Have text and controls be painted smoothly over bitmap
  399.     // without using default background color
  400.     pDC->SetBkMode( TRANSPARENT );
  401.     return m_brush;
  402.  
  403.     // Do not call base class, which, sets background color differently
  404.     // return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
  405. }
  406.  
  407.  
  408.  
  409. //***********************************************************************
  410. // IntroScreen::OnDestroy()
  411. //
  412. // Purpose:
  413. //
  414. //        Select old bitmap back into memory DC before it is destroyed
  415. //      when CPaintDlg object is.
  416. //        DeleteObject() the bitmap which had been loaded.
  417. //
  418. // Parameters:
  419. //
  420. //        None.
  421. //
  422. // Returns:
  423. //
  424. //        None.
  425. //
  426. // Comments:
  427. //
  428. // History:
  429. //
  430. //   Date   Comment                                           Initials
  431. // ======== ================================================= ========
  432. //***********************************************************************
  433. void introscreen::OnDestroy()
  434. {
  435.     CDialog::OnDestroy();
  436.     
  437.     // Select old bitmap into memory dc (selecting out circle bitmap)
  438.     // Need to create a temporary pointer to pass to do this
  439.     ASSERT( m_hBmpOld );
  440.     VERIFY( m_dcMem.SelectObject( CBitmap::FromHandle(m_hBmpOld) ) );
  441.     
  442.     // Need to DeleteObject() bitmap which was loaded
  443.     m_bmpCircle.DeleteObject();
  444.  
  445.     // m_dcMem destructor will handle rest of cleanup    
  446. }
  447.  
  448.  
  449. //***********************************************************************
  450. // CPaintDlg::OnInitDialog()
  451. //
  452. // Purpose:
  453. //
  454. //        Create compatible memory DC for dialog.
  455. //        Load bitmap resource.  Get bitmap size.
  456. //        Select bitmap into memory DC and store handle to old bitmap.
  457. //        Calculate size of dialogs client area for later StretchBlt()
  458. //        of bitmap.
  459. //        Load a hollow brush for later transparent painting of 
  460. background
  461. //        caused by modified OnCtlColor() handler.
  462. //
  463. // Parameters:
  464. //
  465. //        None.
  466. //
  467. // Returns:
  468. //
  469. //        BOOL    (TRUE unless focus set to control)
  470. //
  471. // Comments:
  472. //
  473. // History:
  474. //
  475. //   Date   Comment                                           Initials
  476. // ======== ================================================= ========
  477. // 10/30/93 Created                                             TF
  478. //***********************************************************************
  479. /////////////////////////////////////////////////////////////////////////
  480. ////
  481. // IntroScreen message handlers
  482.  
  483. BOOL introscreen::OnInitDialog()
  484. {
  485.     CDialog::OnInitDialog();
  486.     
  487.     CBitmap * pBmpOld;
  488.     RECT rectClient;
  489.     
  490.     // Load default brush;
  491.     VERIFY( m_brush = (HBRUSH)GetStockObject( HOLLOW_BRUSH ) );
  492.     
  493.     // Load bitmap resource - remember to call DeleteObject when done.
  494.     VERIFY( m_bmpCircle.LoadBitmap( IDB_BITMAP10 ) );
  495.  
  496.     // Get bitmap information    
  497.     m_bmpCircle.GetObject( sizeof(BITMAP), &m_bmInfo );
  498.  
  499.     GetClientRect( &rectClient ); 
  500.     
  501.     m_size.cx = rectClient.right;    // zero based
  502.     m_size.cy = rectClient.bottom;    // zero based
  503.     m_pt.x = rectClient.left;
  504.     m_pt.y = rectClient.top;
  505.     
  506.     // Get temporary DC for dialog - Will be released in dc destructor
  507.     CClientDC dc(this);
  508.     
  509.     // Create compatible memory DC using the dialogs DC
  510.     VERIFY( m_dcMem.CreateCompatibleDC( &dc ) );
  511.  
  512.     // Select circle bitmap into DC.  
  513.     // Get pointer to original bitmap
  514.     // NOTE! This is temporary - save the handle instead
  515.     VERIFY( pBmpOld = m_dcMem.SelectObject( &m_bmpCircle ) );
  516.     
  517.     VERIFY( m_hBmpOld = (HBITMAP) pBmpOld->GetSafeHandle() );
  518.  
  519.     // local CCLientDC destructor for 'dc' will release device context   
  520.  
  521.     button1 = new CBitmapButton(); 
  522.     button2 = new CBitmapButton(); 
  523.     button3 = new CBitmapButton(); 
  524.     button4 = new CBitmapButton(); 
  525.  
  526.     VERIFY(button1->AutoLoad(IDC_IDDP, this)); //this
  527.     button1->SizeToContent();
  528. #undef pete
  529. #ifdef pete    
  530.     VERIFY(button2->AutoLoad(IDC_IDITR, this));
  531.     button2->SizeToContent();
  532.     VERIFY(button3->AutoLoad(IDC_IDINS, this));
  533.     VERIFY(button4->.AutoLoad(IDC_IDST, this));
  534.     button3->SizeToContent();
  535.     button4->SizeToContent();
  536. #endif
  537. //    button1.EnableWindow(TRUE);
  538. //    button2.EnableWindow(TRUE);
  539. //    button3.EnableWindow(TRUE);
  540. //    button4.EnableWindow(TRUE);
  541.  
  542.     return TRUE;
  543. }
  544.  
  545.  
  546. //***********************************************************************
  547. // IntroScreen::OnEraseBkgnd()
  548. //
  549. // Purpose:
  550. //
  551. //        Paint background of dialog.  In this case, use StretchBlt()
  552. //        to paint a bitmap onto it.
  553. //
  554. // Parameters:
  555. //
  556. //        None.
  557. //
  558. // Returns:
  559. //
  560. //        BOOL    (TRUE if no more background painting needed)
  561. //
  562. // Comments:
  563. //
  564. // History:
  565. //
  566. //   Date   Comment                                           Initials
  567. // ======== ================================================= ========
  568. // 
  569. //***********************************************************************
  570. BOOL introscreen::OnEraseBkgnd(CDC* pDC)
  571. {
  572.     // StretchBlt Circle bitmap onto static's client area
  573. //    pDC->StretchBlt( 240, m_pt.y + 80, m_size.cx - 240, m_size.cy - 80, 
  574. //                     &m_dcMem, 0, 0, m_bmInfo.bmWidth-1, 
  575. m_bmInfo.bmHeight-1,
  576. //                     SRCCOPY );
  577.     pDC->StretchBlt( 0, 0, m_size.cx, m_size.cy, 
  578.                      &m_dcMem, 0, 0, m_bmInfo.bmWidth-1, 
  579. m_bmInfo.bmHeight-1,
  580.                      SRCCOPY );
  581.     /*
  582.      * First two parameters are upper left position to place bitmap.
  583.      * Third and fourth parameters are width and height to copy to.
  584.      * (could be less than actual size of bitmap)
  585.      * Sixth and seventh are position in memory dc to start from
  586.      * 8th and 9th are size of bitmap to copy from
  587.      * SRCCOPY specifies copy.
  588.      * See StretchBlt documentation for more details.
  589.      */
  590.  
  591.     return TRUE;    // No more background painting needed    
  592. }
  593.  
  594.  
  595. int introscreen::OnCreate(LPCREATESTRUCT lpCreateStruct)
  596. {
  597.     if (CDialog::OnCreate(lpCreateStruct) == -1)
  598.         return -1;
  599.     
  600.     // TODO: Add your specialized creation code here
  601.     
  602.     return 0;
  603. }
  604.  
  605. void introscreen::OnShowWindow(BOOL bShow, UINT nStatus)
  606. {
  607.     CDialog::OnShowWindow(bShow, nStatus);
  608.     
  609.     // TODO: Add your message handler code here
  610.     
  611. }
  612.  
  613.  
  614.